home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 14077 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.6 KB  |  74 lines

  1. Path: anvil.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c,comp.unix.programmer
  4. Subject: Re: Q: '\n' character
  5. Date: 11 Apr 1996 10:55:17 -0700
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4kjh25INNf2e@anvil.ugrad.cs.ubc.ca>
  8. References: <4kj66f$k0o@ren.cei.net>
  9. NNTP-Posting-Host: anvil.ugrad.cs.ubc.ca
  10.  
  11. In article <4kj66f$k0o@ren.cei.net>,  <james lemley> wrote:
  12. >>>>Is there a function or some sort of way that I could remove '\n'
  13. >>>>charecter form the end of the string. 
  14. >
  15. >fgets(buffer, file); 
  16. >buffer[strlen(buffer)-1]=0;  
  17. >
  18. >
  19. >It may not be the fastest, but it is darned near the easiest. 
  20.  
  21. It's also completely incorrect. What if there is no newline at the end?
  22.  
  23. This is just part of a bullshit solution to the original problem which is to
  24. print lines from two files side by side. 
  25.  
  26. In implementing the solution, there is no need to build a string and then try
  27. to remove a trailing newline.
  28.  
  29. Here is one solution that doesn't use its own buffers at all, though admittedly
  30. it does once call feof() on the file streams before anything is read from them
  31. for the sake of keeping everything in one tidy loop, rather than splitting
  32. into cases:
  33.  
  34.  
  35. #include <stdio.h>
  36.  
  37. void concatprint(FILE *f1, FILE *f2)
  38.  
  39. /*
  40.  * read lines from f1 and f2 simultaneously, and print them side by side
  41.  * f1 and f2 must be valid streams open for reading.
  42.  */
  43.  
  44. {
  45.         int c;
  46.  
  47.         do {
  48.                 if (!feof(f1) && !ferror(f1)) 
  49.                         while ((c = getc(f1)) != '\n' && c != EOF)
  50.                                 putchar(c);
  51.                 if (!feof(f2) && !ferror(f2)) 
  52.                         while ((c = getc(f2)) != '\n' && c != EOF)
  53.                                 putchar(c);
  54.                 putchar('\n');
  55.         } while ((!feof(f1) && !ferror(f1)) || (!feof(f2) && !ferror(f2)));
  56. }
  57.  
  58. This is a complete, tested solution to the original posting, unlike some of the
  59. half-baked, unverified remarks that were posted in response, which completely
  60. miss the central problem anyway. There were calls for strtok(), fgets() and
  61. other nonsense, most of which were shown incorrect, anyway. That's like if I
  62. went to a doctor with my own ad-hoc diagnosis, and he prescribed a remedy for
  63. what I think is wrong with me instead of diagnosing.
  64.  
  65. Patient: ``Doctor, I think I need to strip a newline from a string to make my
  66. program work!'' 
  67.  
  68. Doctor: ``I won't bother looking at what your program is trying to do despite
  69. that you brought it with you; you obviously know what you are doing, and just
  70. need a little pointer. Here is a prescription for removing a newline that will
  71. sometimes work...'' 
  72. -- 
  73.  
  74.